package com.zz.slick;

import java.util.ArrayList;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.GameState;
import org.newdawn.slick.state.StateBasedGame;

import com.zz.art.Art;
import com.zz.entity.Player;
import com.zz.entity.PlayerList;
import com.zz.level.Level;
import com.zz.level.Level1;
import com.zz.pathfinding.Path;
import com.zz.tiles.Tile;

public class Play extends BasicGameState implements GameState {

	public ArrayList<Player> players;
	public Player player;
	public Level level;
	public Path path;
	public String message = "1";
	int mouseX;
	int mouseY;
	private boolean pLoaded;
	
		
	public Play(int play) {
		
	}

	

	@Override
	public void mouseMoved(int oldx, int oldy, int newx, int newy) {
		handleMouseMoved(newx, newy);
	}

	@Override
	public void mousePressed(int button, int x, int y) {
		handleMousePressed(x, y);
	}

	@Override
	public void mouseReleased(int button, int x, int y) {

	}

	@Override
	public void mouseWheelMoved(int arg0) {

	}
	
	private void handleMouseMoved(int x, int y) {
		x /= Tile.size;
		y /= Tile.size;
		mouseX = x;
		mouseY = y;
		message = x +", " + y;
		//message = String.valueOf(level.getNeighbours(x, y));
		if ((x < 0) || (y < 0) || (x >= level.getWidthInTiles()) || (y >= level.getHeightInTiles())) {
			return;
		}
		
		if (level.selectedx != -1) {
			if ((level.lastFindX != x) || (level.lastFindY != y)) {
				level.lastFindX = x;
				level.lastFindY = y;
				player.path = player.finder.findPath(new Player(level.getUnit(level.selectedx, level.selectedy)), 
						level.selectedx, level.selectedy, x, y);
			}
		}
	}
	
	private void handleMousePressed(int x, int y) {
		x /= Tile.size;
		y /= Tile.size;
		
		if ((x < 0) || (y < 0) || (x >= level.getWidthInTiles()) || (y >= level.getHeightInTiles())) {
			return;
		}
		
		if (level.getUnit(x, y) != null) {
			level.selectedx = x;
			level.selectedy = y;
			level.lastFindX = - 1;
			player = level.getUnit(x, y);
		} else {
			if (level.selectedx != -1) {
				level.clearVisited();
				player.path = player.finder.findPath(new Player(level.getUnit(level.selectedx, level.selectedy)), 
						level.selectedx, level.selectedy, x, y);
				
				if (path != null) {
					level.getUnit(level.selectedx, level.selectedy).path = path;
					level.getUnit(level.selectedx, level.selectedy).finished = false;
					path = null;
					/*Entity unit = level.getUnit(level.selectedx, level.selectedy);
					level.setUnit(level.selectedx, level.selectedy, null);
					level.setUnit(x,y,unit);*/
					level.selectedx = -1;
					level.selectedy = -1;
					level.lastFindX = - 1;
				}
			}
		}
	}
	
	@Override
	public int getID() {
		return 2;
	}

	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		gc.setMinimumLogicUpdateInterval(100);
		gc.setMouseCursor(Art.cursor, 0, 0);
		//gc.setMouseGrabbed(true);
		gc.setShowFPS(false);
		pLoaded = false;
		Art.loadImages();
		level = new Level1();
	}

	@Override
	public void leave(GameContainer arg0, StateBasedGame arg1) throws SlickException {

	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		Art.bg.draw(0, 0);
		level.draw(path, g);
		
		g.drawString(message, 0, 0);
		//g.drawImage(Art.cursor, Mouse.getX(), Options.getResY() - Mouse.getY());
		if(mouseX > 0 && mouseX < level.width - 1 &&  mouseY > 0 &&  mouseY < level.height -1)
			if(level.getUnit(mouseX,  mouseY) != null)
				g.drawString(level.getUnit(mouseX,  mouseY).name + "\n" + level.getUnit(mouseX,  mouseY).finder.toString(), Mouse.getX(), Options.getResY() - Mouse.getY());
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		if(!pLoaded) {
			players = level.getPlayers(LevelSelect.players);
			System.out.println(players.get(0).name);
			pLoaded = true;
		}
		if(player != null)
			player.move(level);		
	}

}
